package com.guli.gulimall.gulimallproduct.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReentrantReadWriteLock;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.guli.gulimall.gulimallproduct.entity.CategoryEntity;
import com.guli.gulimall.gulimallproduct.service.CategoryService;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.R;



/**
 * 商品三级分类
 *
 * @author zouzhi
 * @email 1245268075@qq.com
 * @date 2022-04-03 10:19:09
 */
@RestController
@RequestMapping("gulimallproduct/category")
@Api(value = "CategoryController接口",tags = "商品三级分类接口")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    private static final ReentrantReadWriteLock reentrantReadWriteLoc;

    static {
        reentrantReadWriteLoc = new ReentrantReadWriteLock();
    }
    /**
     * 查询所有分类以及子分类，以树形结构组装起来
     */
    @GetMapping("/list/tree")
    @ApiOperation(value = "商品分类接口")
    public R list(){
        List<CategoryEntity>  entities = categoryService.listWithTree();
        return R.ok().put("data", entities);
    }
    /**
     * 信息
     */
    @ApiOperation(value = "根据catId商品编号查询商品")
    @GetMapping("/info/{catId}")
    public R info(@PathVariable("catId") Long catId){
		CategoryEntity category = categoryService.getById(catId);

        return R.ok().put("data", category);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增商品")
    public R save(@RequestBody CategoryEntity category){
        reentrantReadWriteLoc.writeLock().lock();
        boolean b =false;
        try{
            b = categoryService.save(category);
        }catch (Exception E){
            System.out.println(E);
        }finally {
            reentrantReadWriteLoc.writeLock().unlock();
        }
        return b?R.ok():R.error();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改并保存商品")
    public R update(@RequestBody CategoryEntity category){
        reentrantReadWriteLoc.writeLock().lock();
        boolean b =false;
        try{
            b = categoryService.updateCascade(category);
        }catch (Exception E){
            System.out.println(E);
        }finally {
            reentrantReadWriteLoc.writeLock().unlock();
        }
        return  b?R.ok():R.error();
    }
    /**
     * 改变商品排序和层级关系
     */
    @PostMapping("/update/sort")
    @ApiOperation(value = "改变商品排序和层级关系")
    public R updateSort(@RequestBody CategoryEntity[] category){
        reentrantReadWriteLoc.writeLock().lock();
        boolean b =false;
        try{
            b = categoryService.updateBatchById(Arrays.asList(category));

        }catch (Exception E){
            System.out.println(E);
        }finally {
            reentrantReadWriteLoc.writeLock().unlock();
        }
        return  b?R.ok():R.error();
    }
    /**
     * 删除
     */
    @PostMapping("/delete")
    @ApiOperation(value = "商品批量删除接口")
    public R delete(@RequestBody Long[] catIds){
        reentrantReadWriteLoc.writeLock().lock();
        try {
            return categoryService.removeMenuByIds(Arrays.asList(catIds));
        }catch (Exception e){
            System.out.println(e);
        }finally {
            reentrantReadWriteLoc.writeLock().unlock();
        }
       return  R.error().put("data",null);
    }

}
